﻿#include "userinfowidget.h"
#include "ui_userinfowidget.h"
#include "chatsocket.h"
#include "verifywidget.h"

UserInfoWidget::UserInfoWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UserInfoWidget)
{
    ui->setupUi(this);
    setShadow();
    //加载图标
    ui->label_9->setPixmap(QPixmap("res/Icon/info.png"));
    ui->label_2->setPixmap(QPixmap("res/Icon/joker2.png"));
    ui->label_5->setPixmap(QPixmap("res/Icon/person.png"));
}

UserInfoWidget::~UserInfoWidget()
{
    delete ui;
}

void UserInfoWidget::setShadow()
{
    //设置无边框
    setWindowFlags(Qt::FramelessWindowHint);
    //设置窗体透明
    setAttribute(Qt::WA_TranslucentBackground, true);
    QGraphicsDropShadowEffect *shadow=new QGraphicsDropShadowEffect();
    shadow->setBlurRadius(15);//设置圆角半径
    shadow->setColor(QColor(0,0,0));//设置边框颜色
    shadow->setOffset(0,0);
    ui->widget->setStyleSheet("background-color:white");
    ui->widget->setGraphicsEffect(shadow);
}

void UserInfoWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
        isPressedWidget = true; // 当前鼠标按下的是QWidget而非界面上布局的其它控件
        last = event->globalPos();
    }
}

void UserInfoWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isPressedWidget)
    {
        int dx = event->globalX() - last.x();
        int dy = event->globalY() - last.y();
        last = event->globalPos();
        move(x()+dx, y()+dy);
    }
}

void UserInfoWidget::mouseReleaseEvent(QMouseEvent *event)
{
    int dx = event->globalX() - last.x();
    int dy = event->globalY() - last.y();
    move(x()+dx, y()+dy);
    isPressedWidget = false; // 鼠标松开时，置为false
}

void UserInfoWidget::initInfo(const UserInfo &userInfo, bool isFriend)
{
    m_userInfo = userInfo;
    if(isFriend)
    {
        ui->pushButton->setText("发消息");
    }
    else
    {
        ui->pushButton->setText("加好友");
    }
    //填充信息
    ui->label_3->setText(userInfo.id);
    ui->label_4->setText(userInfo.sex + "   "+ userInfo.age + "岁   " + userInfo.birthday + "   " + userInfo.star + "   " + userInfo.zodiac);

    if(userInfo.phone.size())
        ui->label_8->setText("电话: " + userInfo.phone);
    else
        ui->label_8->setText("电话: 用户未填写");

    if(userInfo.address.size())
        ui->label_10->setText("家乡: " + userInfo.address);
    else
        ui->label_10->setText("家乡: 用户未填写");

    if(userInfo.rubric.size())
        ui->label_11->setText("个人说明: " + userInfo.rubric);
    else
        ui->label_11->setText("个人说明: 用户未填写");

    QString path = QCoreApplication::applicationDirPath();
//    QString realHeadPath = QString("%1/%2").arg(path).arg(userInfo.headurl);
    ui->label_head->setPixmap(QPixmap(userInfo.headurl));
//    ui->label_head->setStyleSheet(QString("border-image: url(%1);border-radius:25px;").arg(realHeadPath));
    ui->label_nickname->setText(userInfo.nickname);
    ui->label_persig->setText(userInfo.persig);
    ui->label_bk->setPixmap(QPixmap("res/img/bk5.jpg"));
    ui->label_bk->move(0,0);
    ui->widget_2->setStyleSheet("#widget_2{border-right:1px solid  rgb(0,0,0);}");
    //    ui->widget_2->setStyleSheet("#widget_2{border-image:url(res/img/bk5.jpg)}");
    QString ID = ChatSocket::getInstance()->getUserID();
    if(ID == userInfo.id)
        setBtnHide();
}

void UserInfoWidget::setBtnHide()
{
    ui->pushButton->hide();
}

void UserInfoWidget::setBtnText(const QString& text)
{
    ui->pushButton->setText(text);
}


void UserInfoWidget::on_pushButton_2_clicked()
{
    close();
}

void UserInfoWidget::on_pushButton_3_clicked()
{
    hide();
}

void UserInfoWidget::on_pushButton_clicked()// 加好友/发消息响应
{
    if(ui->pushButton->text()=="加好友")
    {
        VerifyWidget* vw = new VerifyWidget;
        vw->initAllInfo(m_userInfo);
        vw->show();
        return ;
    }
    else if(ui->pushButton->text()=="发消息")
    {
        //若原来有此聊天窗口则将其显示
        for(auto obj : ChatSocket::getInstance()->m_chatList)
        {
            QString str = obj->getWinName();
            if(str.contains(m_userInfo.id))
            {
                obj->raise();
                obj->show();
                return;
            }
        }
        //否则创建
        ChatWidget* cw = new ChatWidget;
        cw->setWinName(m_userInfo.nickname+"("+m_userInfo.id+")");
        cw->show();
        ChatSocket::getInstance()->m_chatList.append(cw);
        //查找有无与其的聊天记录
        //查找有无聊天记录
        QString str(m_userInfo.nickname+"("+m_userInfo.id+")");
        int i = str.indexOf('(');
        str =str.replace(")","");
        str = str.mid(i+1);
        QString path = QCoreApplication::applicationDirPath();
        QString uid = Widget::getInstance()->getID();
        QString filename = QString("%1/%2/%3-%4.txt").arg(path).arg(uid).arg(uid).arg(str);
        QFile file(filename);
        if(file.exists())
        {
            if(!file.open(QIODevice::ReadOnly))
            {
                QMessageBox::warning(this,"警告","打开文件失败");
                return;
            }
            char info[1024];
            char info2[1024];
            while(!file.atEnd())
            {
                memset(info,0,1024);
                memset(info2,0,1024);
                file.readLine(info,1024);
                QString msg2="";
                while(1)
                {
                    file.readLine(info2,1024);
                    msg2 += QString(info2);
                    QString content(info2);
                    if(content.endsWith("わ\r\n"))
                    {
                        break;
                    }
                }
                QString msg = QString(info)+"&" + msg2;
                cw->restoreChatLog(msg);
            }
            file.close();
        }
        return;
    }
}
